package atomgit_hook

import (
	"time"

	"atomgit.com/openlinksaas-org/proto-gen-go.git/events/events_atomgit"
)

type GitUser struct {
	Email    string `json:"email"`
	Name     string `json:"name"`
	Username string `json:"username"`
}

func (item *GitUser) ToProtoBuf() *events_atomgit.GitUser {
	return &events_atomgit.GitUser{
		Email:    item.Email,
		Name:     item.Name,
		Username: item.Username,
	}
}

type User struct {
	AvatarUrl string `json:"avatar_url"`
	HtmlUrl   string `json:"html_url"`
	Email     string `json:"email"`
	Id        string `json:"id"`
	Login     string `json:"login"`
	Name      string `json:"name"`
	Type      string `json:"type"`
}

func (item *User) ToProtoBuf() *events_atomgit.User {
	return &events_atomgit.User{
		AvatarUrl: item.AvatarUrl,
		HtmlUrl:   item.HtmlUrl,
		Email:     item.Email,
		Id:        item.Id,
		Login:     item.Login,
		Name:      item.Name,
		Type:      item.Type,
	}
}

type Repository struct {
	AllowForking    bool    `json:"allow_forking"`
	Archived        bool    `json:"archived"`
	CloneUrl        string  `json:"clone_url"`
	CreatedAt       string  `json:"created_at"`
	DefaultBranch   string  `json:"default_branch"`
	Description     string  `json:"description"`
	Disabled        bool    `json:"disabled"`
	Fork            bool    `json:"fork"`
	Forks           uint32  `json:"forks"`
	ForksCount      uint32  `json:"forks_count"`
	FullName        string  `json:"full_name"`
	GitUrl          string  `json:"git_url"`
	Homepage        string  `json:"homepage"`
	HtmlUrl         string  `json:"html_url"`
	Id              int64   `json:"id"`
	IsTemplate      bool    `json:"is_template"`
	MasterBranch    string  `json:"master_branch"`
	Name            string  `json:"name"`
	Owner           User    `json:"owner"`
	Private         bool    `json:"private"`
	Size            float32 `json:"size"`
	SshUrl          string  `json:"ssh_url"`
	StargazersCount uint32  `json:"stargazers_count"`
	UpdatedAt       string  `json:"updated_at"`
	Url             string  `json:"url"`
	Visibility      string  `json:"visibility"`
}

func (item *Repository) ToProtoBuf() *events_atomgit.Repository {
	createTime := int64(0)
	t, err := time.Parse(time.RFC3339, item.CreatedAt)
	if err == nil {
		createTime = t.UnixNano() / 1e6
	}
	updateTime := int64(0)
	t, err = time.Parse(time.RFC3339, item.UpdatedAt)
	if err == nil {
		updateTime = t.UnixNano() / 1e6
	}
	return &events_atomgit.Repository{
		AllowForking:    item.AllowForking,
		Archived:        item.Archived,
		CloneUrl:        item.CloneUrl,
		CreatedAt:       createTime,
		DefaultBranch:   item.DefaultBranch,
		Description:     item.Description,
		Disabled:        item.Disabled,
		Fork:            item.Fork,
		Forks:           item.Forks,
		ForksCount:      item.ForksCount,
		FullName:        item.FullName,
		GitUrl:          item.GitUrl,
		Homepage:        item.Homepage,
		HtmlUrl:         item.HtmlUrl,
		Id:              item.Id,
		IsTemplate:      item.IsTemplate,
		MasterBranch:    item.MasterBranch,
		Name:            item.Name,
		Owner:           item.Owner.ToProtoBuf(),
		Private:         item.Private,
		Size:            item.Size,
		SshUrl:          item.SshUrl,
		StargazersCount: item.StargazersCount,
		UpdatedAt:       updateTime,
		Url:             item.Url,
		Visibility:      item.Visibility,
	}
}
